package su.security.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import net.kuakao.core.dto.PagerDTO;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import su.base.Constants;
import su.base.dao.SuBaseDAO;
import su.security.dao.ISecurityUserDAO;
import su.security.entity.SecurityUser;
import su.tool.Md5;


@Repository
public class SecurityUserDAO extends SuBaseDAO implements ISecurityUserDAO {
	
	public List<SecurityUser> queryByPage(PagerDTO s)throws SQLException{
		StringBuilder sql = new StringBuilder("select * from su_security_user where status=1");
		if(StringUtils.isNotBlank(s.getS1())){//id
			if(s.getS1().indexOf(",")>-1){
				sql.append(" and id in ("+s.getS1()+")");
			}else{
				sql.append(" and id="+s.getS1());
			}
			
		}
		if(StringUtils.isNotBlank(s.getS2()))sql.append(" and username='"+s.getS2()+"'");//用户名
		
		sql.append(" ORDER BY id desc");
		return super.queryByPage(sql.toString(), new RowMapper<SecurityUser>() {

			public SecurityUser mapRow(ResultSet rs, int arg1) throws SQLException {
				SecurityUser entity = new SecurityUser();
				entity.setId(rs.getInt("id"));
				entity.setModuleids(rs.getString("moduleids"));
				entity.setStatus(rs.getInt("status"));
				entity.setUsername(rs.getString("username"));
				entity.setUserpwd(rs.getString("userpwd"));
				entity.setUsertype(rs.getInt("usertype"));
				return entity;
			}
			
		}, s);
	}
	
	public SecurityUser getBaseinfo(int id)throws SQLException{
		String sql = "select * from su_security_user where status=1 and id=?";
		return super.queryForObject(sql,new Object[]{id}, new RowMapper<SecurityUser>() {

			public SecurityUser mapRow(ResultSet rs, int arg1) throws SQLException {
				SecurityUser entity = new SecurityUser();
				entity.setId(rs.getInt("id"));
				entity.setModuleids(rs.getString("moduleids"));
				entity.setStatus(rs.getInt("status"));
				entity.setUsername(rs.getString("username"));
				entity.setUserpwd(rs.getString("userpwd"));
				entity.setUsertype(rs.getInt("usertype"));
				return entity;
			}
			
		});
	}
	
	public SecurityUser login(String username,String userpwd) throws SQLException{
		String sql = "select * from su_security_user where status=1 and username='"+username+"' and userpwd='"+Md5.md5(userpwd)+"'";
		return super.queryForObject(sql, new RowMapper<SecurityUser>() {

			public SecurityUser mapRow(ResultSet rs, int arg1) throws SQLException {
				SecurityUser entity = new SecurityUser();
				entity.setId(rs.getInt("id"));
				entity.setModuleids(rs.getString("moduleids"));
				entity.setStatus(rs.getInt("status"));
				entity.setUsername(rs.getString("username"));
				entity.setUserpwd(rs.getString("userpwd"));
				entity.setUsertype(rs.getInt("usertype"));
				return entity;
			}
			
		});
	}
	
	public void insert(SecurityUser o)throws SQLException{
		String sql = "insert into su_security_user(username,userpwd,status,usertype,moduleids) values(?,?,?,?,?)";
		super.update(sql, new Object[]{o.getUsername(),o.getUserpwd(),o.getStatus(),o.getUsertype(),o.getModuleids()});
	}
	public void update(SecurityUser o)throws SQLException{
		String sql = "update su_security_user set username=?,usertype=?,moduleids=? where id=?";
		super.update(sql, new Object[]{o.getUsername(),o.getUsertype(),o.getModuleids(),o.getId()});
	}
	public void remove(int id)throws SQLException{
		String sql = "update su_security_user set status=? where id=?";
		super.update(sql, new Object[]{Constants.STATUS_DISABLE,id});
	}
	public void operateResetPwd(String userpwd,int userId)throws SQLException{
		String sql = "update su_security_user set userpwd=? where id=?";
		super.update(sql, new Object[]{userpwd,userId});
	}
}

